package com.aaa.controller.mht;

import com.aaa.entity.LayData;
import com.aaa.entity.MyPage;
import com.aaa.entity.mht.MhtQuestion;
import com.aaa.service.mht.MhtService;
import com.aaa.util.EasyExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("mht")
public class MhtTitleController {
    @Autowired
    private MhtService mhtService;
    /**
     * 导入excel数据mht
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("excelFile")
    @ResponseBody
    public LayData mhtUploadExcel(MultipartFile file) throws IOException {

        List<Object> list = EasyExcelUtil.readExcel(file, new MhtQuestion(), 1,1);
        Boolean isNO = mhtService.inductsExcelFile(list);
        return new LayData(isNO);
    }
    /**
     * 展示所有的题目信息
     * @param myPage
     * @return
     */
    @RequestMapping("showQuestion")
    @ResponseBody
    public LayData mhtShowQuestion(MyPage myPage,MhtQuestion mhtQuestion){
        PageInfo questionslist = mhtService.findQuestions(myPage,mhtQuestion);
        return new LayData(questionslist);
    }
    /**
     * 检测题目的id是否已经存在
     */
    @PostMapping("checkTitleId{titleid}")
    @ResponseBody
    public LayData checkTitleId(@PathVariable Integer titleid){
        Boolean isno = mhtService.checkTitleId(titleid);
        return new LayData(isno);
    }

    /**
     * 添加题目信息
     * @param mhtQuestion
     * @return
     */
    @PostMapping("addQuestions")
    @ResponseBody
    public LayData insertQuestions(MhtQuestion mhtQuestion){
        Boolean isno = mhtService.addQuestion(mhtQuestion);
        return new LayData(isno);
    }
    /**
     * 删除题目信息
     */
    @PostMapping("delQuestion")
    @ResponseBody
    public LayData delQuestion(MhtQuestion mhtQuestion){
        Boolean isno = mhtService.delQuestion(mhtQuestion);
        return new LayData(isno);
    }
    /**
     * 获取所有的题目信息（做题页面使用）
     */
    @RequestMapping("index")
    public String findAllTile(MyPage myPage, HttpServletRequest request){
        myPage.setPage(1);
        myPage.setLimit(99);
        PageInfo allTile = mhtService.findAllTile(myPage);
        request.setAttribute("allsub",allTile);
        return "mht/index";
    }
}
